package main

import (
	"fmt"
	"math/rand"
	"strings"
	"time"

	"github.com/aliyun/alibaba-cloud-sdk-go/services/dysmsapi"
)

// GenerateSMSCode 生成长度为length的随机数
func GenerateSMSCode(length int) string {
	numeric := [10]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
	r := len(numeric)
	rand.Seed(time.Now().UnixNano())

	var sb strings.Builder

	for i := 0; i < length; i++ {
		fmt.Fprintf(&sb, "%d", numeric[rand.Intn(r)])
	}
	return sb.String()
}

func main() {
	client, err := dysmsapi.NewClientWithAccessKey("ap-northeast-1", "LTAI4FtVBxHxMvqVTrSRVVGR", "qZKe7BovYSTN55h6B2E3wiFjc8K5eT")
	if err != nil {
		panic(err)
	}

	request := dysmsapi.CreateSendSmsRequest()
	request.Scheme = "https"

	request.PhoneNumbers = "18978057553"
	request.SignName = "帮福共享科技"
	request.TemplateCode = "SMS_173150562"
	// request.TemplateParam = "{\"code\":\"1111\"}"
	request.TemplateParam = fmt.Sprintf(`{"code":"%s"}`, GenerateSMSCode(4))

	response, err := client.SendSms(request)
	if err != nil {
		fmt.Print(err.Error())
	}

	// 保存验证码

	fmt.Printf("response is %#v\n", response)
}
